const { dbMysql } = require('../../config/config');
const db = require('../../config/config')
module.exports = {
    // ---------------------退款原因
    getReason(req, res) {
        let sql = `select * from reason where ifDel=0`
        db.dbMysql(sql, [], function (err, data) {
            res.send({
                code: 200,
                data: data,
                // count: data.length,
                msg: '查询成功'
            })
        })
    },
    // 更改启用状态
    changeState(req, res) {
        let id = req.body.id
        let flag = req.body.state
        let sql = ''
        if (flag) {
            sql = `update reason set reason_state = 0 where id=${id}`
        } else {
            sql = `update reason set reason_state = 1 where id=${id}`
        }

        db.dbMysql(sql, [], function (err, data) {
            res.send({
                code: 200,
                msg: '查询成功'
            })
        })
    },
    // 筛选操作
    selectReason(req, res) {
        let reason_state = req.body.reason_state
        let reason = req.body.reason
        let sql = ''
        if (reason_state == 2) {
            sql = `select * from reason where reason like '%${reason}%'`
        } else {
            sql = `select * from reason where reason like '%${reason}%' and reason_state=${reason_state}`

        }
        db.dbMysql(sql, [], function (err, data) {
            res.send({
                code: 200,
                data: data,
                msg: '查询成功'
            })
        })
    },
    // 编辑
    eidtReason(req, res) {
        let id = req.body.id
        let reason = req.body.reason
        let sql = `update reason set reason=? where id=?`
        db.dbMysql(sql, [reason, id], function (err, data) {
            res.send({
                code: 200,
                // data: data,
                msg: '查询成功'
            })
        })
    },
    // 添加
    addReason(req, res) {
        let reason_create_time = req.body.reason_create_time
        let reason = req.body.reason
        let sql = `insert into reason set reason = ?,reason_create_time=?;`
        db.dbMysql(sql, [reason, reason_create_time], function (err, data) {
            res.send({
                code: 200,
                msg: '添加成功'
            })
        })
    },
    // 批量删除
    delReason(req, res) {
        let nums = req.body.id
        // let reason = req.body.reason
        // let sql = `update `
        let sql = `delete from reason where id in(${nums});`
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.send({
                    code: 500,
                    msg: '失败删除'
                })
            } else {
                res.send({
                    code: 200,
                    msg: '删除成功'
                })
            }

        })
    },

    // ---------------退货退款------
    // 退货
    getRefundGood(req, res) {
        let sql = `SELECT refund.*,u1.user_name as userName,u1.user_phone,orderinfo.orderId
        FROM refund 
        INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
        INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
        where (refund.deal_type=1 or refund.deal_type=2) and refund.refund_state=0`
        // let sql = `SELECT refund.*,u1.user_name as userName,u2.user_name as dealName,u1.user_phone,orderinfo.orderId
        // FROM refund 
        // INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
        // INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
        // INNER JOIN user as u2 ON refund.key_deal_id = u2.user_id  where (refund.deal_type=1 or refund.deal_type=2) and refund.refund_state=0 `
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.send({
                    code: 500,
                    msg: '查询失败'
                })
            } else {
                res.send({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            }

        })
    },
    // 退货筛选
    selectGood(req, res) {
        let keyWord = req.body.keyWords
        let phone = req.body.userName
        let username = req.body.userName
        let sql = ``
        if (phone == '' && keyWord != '') {
            // sql = `SELECT refund.*,u1.user_name as userName,u2.user_name as dealName,u1.user_phone,orderinfo.orderId
            // FROM refund 
            // INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
            // INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
            // INNER JOIN user as u2 ON refund.key_deal_id = u2.user_id  where (refund.deal_type=1 or refund.deal_type=2) and refund.refund_state=0 and key_order_id = ${keyWord};`
            sql = `SELECT refund.*,u1.user_name as userName,u1.user_phone,orderinfo.orderId
            FROM refund 
            INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
            INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
            where (refund.deal_type=1 or refund.deal_type=2) and refund.refund_state=0 and key_order_id = ${keyWord};`
        } else if (phone != '' && keyWord == '') {
            sql = `SELECT refund.*,u1.user_name as userName,u1.user_phone,orderinfo.orderId
            FROM refund 
            INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
            INNER JOIN user as u1 ON refund.key_user_id = u1.user_id   where (refund.deal_type=1 or refund.deal_type=2) and refund.refund_state=0 and  (u1.user_phone like '%${phone}%' or u1.user_name like '%${username}%');`
        } else {
            sql = `SELECT refund.*,u1.user_name as userName,u1.user_phone,orderinfo.orderId
            FROM refund 
            INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
            INNER JOIN user as u1 ON refund.key_user_id = u1.user_id where (refund.deal_type=1 or refund.deal_type=2) and refund.refund_state=0 and key_order_id = ${keyWord} and (u1.user_phone like '%${phone}%' or u1.user_name like '%${username}%');`
        }

        db.dbMysql(sql, [], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            } else {

                res.send({
                    code: 500,
                    msg: '查询失败'
                })
            }

        })
    },
    // 退款
    getRefundMoney(req, res) {
        let sql = `SELECT refund.*,u1.user_name as userName,u1.user_phone,orderinfo.orderId
FROM refund 
INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
 where refund.refund_state=0 and refund.deal_type=0;`

        // let sql = `SELECT refund.*,u1.user_name as userName,u2.user_name as dealName,u1.user_phone,orderinfo.orderId
        // FROM refund 
        // INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
        // INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
        // INNER JOIN user as u2 ON refund.key_deal_id = u2.user_id  where refund.refund_state=0 and refund.deal_type=0; `
        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.send({
                    code: 500,
                    msg: '查询失败'
                })
            } else {
                res.send({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            }

        })
    },
    // 退款筛选
    selectMoney(req, res) {
        let keyWord = req.body.keyWords
        let phone = req.body.userName
        let username = req.body.userName
        let sql = ``
        if (phone == '' && keyWord != '') {
            sql = `SELECT refund.*,u1.user_name as userName,u2.user_name as dealName,u1.user_phone,orderinfo.orderId
            FROM refund 
            INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
            INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
            INNER JOIN user as u2 ON refund.key_deal_id = u2.user_id  where refund.deal_type=0 and refund.refund_state=0 and key_order_id = ${keyWord};`
        } else if (phone != '' && keyWord == '') {
            sql = `SELECT refund.*,u1.user_name as userName,u2.user_name as dealName,u1.user_phone,orderinfo.orderId
            FROM refund 
            INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
            INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
            INNER JOIN user as u2 ON refund.key_deal_id = u2.user_id  where refund.deal_type=0 and refund.refund_state=0 and  (u1.user_phone like '%${phone}%' or u1.user_name like '%${username}%');`
        } else {
            sql = `SELECT refund.*,u1.user_name as userName,u2.user_name as dealName,u1.user_phone,orderinfo.orderId
        FROM refund 
        INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
        INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
        INNER JOIN user as u2 ON refund.key_deal_id = u2.user_id  where refund.deal_type=0 and refund.refund_state=0 and key_order_id = ${keyWord} and (u1.user_phone like '%${phone}%' or u1.user_name like '%${username}%');`
        }

        db.dbMysql(sql, [], function (err, data) {
            if (err) {
                res.send({
                    code: 500,
                    msg: '查询失败'
                })
            } else {
                res.send({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            }

        })
    },
    // 添加退款订单
    addOrderRefund(req, res) {
        const { create_time, deal_type, key_user_id, orderId, reason, refund_money } = req.body
        let nums = req.body.id
        // let reason = req.body.reason
        // let sql = `update `
        let sql = `insert into refund set create_time=?,deal_type=?,key_user_id=?,key_order_id=?,reason=?,refund_money=?`
        db.dbMysql(sql, [create_time, deal_type, key_user_id, orderId, reason, refund_money], function (err, data) {
            if (err) {
                res.send({
                    code: 500,
                    msg: '失败删除'
                })
            } else {
                res.send({
                    code: 200,
                    msg: '删除成功'
                })

            }

        })
    },
    // 添加订单后将订单orState改成5
    refund(req, res) {
        let orderId = req.body.orderId
        let sql = `update orderinfo set orState=5 where orderId=?`
        db.dbMysql(sql, [orderId], function (err, data) {
            if (err) {
                res.send({
                    code: 500,
                    msg: '失败删除'
                })
            } else {
                res.send({
                    code: 200,
                    msg: '删除成功'
                })

            }

        })
    },
    // 查询已完成，已拒绝的订单
    selectSuccess(req, res) {
        const orderId = req.body.order_id
        console.log("返回数据", req.body);
        let sql = `SELECT refund.*,u1.user_name as userName,u2.user_name as dealName,u1.user_phone,orderinfo.orderId
        FROM refund 
        INNER JOIN orderinfo ON refund.key_order_id=orderinfo.orderId
        INNER JOIN user as u1 ON refund.key_user_id = u1.user_id 
        INNER JOIN user as u2 ON refund.key_deal_id = u2.user_id  where refund.refund_state=0  and refund.key_order_id=?`

        db.dbMysql(sql, [orderId], function (err, data) {

            if (err) {
                res.send({
                    code: 500,
                    msg: '查询失败'
                })
            } else {
                res.send({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            }

        })
    },
    // 查询已完成，已拒绝的订单
    dealOrderBtn(req, res) {
        const { deal_state, deal_time, remark, suggestion, key_deal_id, order_id } = req.body
        console.log("返回数据", req.body);
        let sql = ``
        if (deal_state == 1 || deal_state == 2) {
            // 修改意见
            sql = `update refund set remark = '${remark}',deal_time='${deal_time}',key_deal_id=${key_deal_id},suggestion='${suggestion}',deal_state=${deal_state} where key_order_id=${order_id} `

        } else {
            sql = `update refund set remark = '${remark}',deal_time='${deal_time}',key_deal_id=${key_deal_id} where key_order_id=${order_id} `
        }

        db.dbMysql(sql, [], function (err, data) {
            console.log("返回数据" + data + '---------' + sql);


            if (err) {
                res.send({
                    code: 500,
                    msg: '查询失败'
                })
            } else {
                res.send({
                    code: 200,
                    msg: '查询成功'
                })
            }

        })
    },
    // 查询订单
      //获取所有订单信息
  getOrder (req, res) {
    db.dbMysql(' SELECT * from orderView where orState in (0,1,2,3);', [], function (err, data) {
       console.log("获取的 订单的数据",data);
        if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '购物车信息查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
}